package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageReservedListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageReservedListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageReservedListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：wsy on 2017/12/26 09:19
 * <p>
 * 邮箱：445979770@qq.com
 *
 * 预留退库列表
 */

public class RefundStorageReservedListActivity extends BaseActivity {
    TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView reservedRefundStorageList;

    private List<RefundStorageReservedListBean.ReferBodyBean> allData;

    private RefundStorageReservedListAdapter adapter;

    private String mReservedNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_reserved_list);
        initView(RefundStorageReservedListActivity.this);
        initData(RefundStorageReservedListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        reservedRefundStorageList = (PullableListView) activity.findViewById(R.id.reservedRefundStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.reservedRefundStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_refund_storage_reserced_list_set_up_list_of_restores));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_main_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        reservedRefundStorageList.setPullUpFlag(false);
        reservedRefundStorageList.setPullDownFlag(false);
        mReservedNumber = "";
        allData = new ArrayList<>();
        addReservedRefundListData(activity, allData, reservedRefundStorageList, true);
    }

    private void addReservedRefundListData(Context context, List<RefundStorageReservedListBean.ReferBodyBean> saleOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new RefundStorageReservedListAdapter(context, saleOutStorageList, mReservedNumber, (String reservedNumber) -> {
                String checkResult = check(reservedNumber);
                if (checkResult == null) {
                    mReservedNumber = reservedNumber;
                    getData(context, mReservedNumber);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String reservedNumber) {
        String result = null;
        if (TextUtils.isEmpty(reservedNumber)) {
            result = getResources().getString(R.string.activity_refund_storage_reserced_list_the_reserved_number_is_not_empty);
        }
        return result;
    }

    private void getData(Context context, String reservedNumber) {
        RefundStorageReservedListRequest.request(context, reservedNumber, new GsonBeanListener<RefundStorageReservedListBean>() {
            @Override
            public void onSuccess(RefundStorageReservedListBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addReservedRefundListData(context, ActivityUtils.getShowData(allData, 0), reservedRefundStorageList, true);
            }
        });
    }

    public interface RefundStorageReservedListener {
        void onNoDoubleClickListener(String reservedNumber);
    }
}
